package com.example.learnvideo.service

import android.app.Service
import android.content.Intent
import android.graphics.Color
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import com.example.learnvideo.MainApplication
import com.example.learnvideo.R
import com.example.learnvideo.widget.FloatWindow

/**
 *
 * @Author： LJH
 * @Time： 2022/7/18
 * @description：
 */
class StockService:Service() {

    private var num1 = 123.22f
    private var num2 = 123.22f
    private lateinit var tvStock1:TextView
    private lateinit var tvStock2:TextView
    private lateinit var mFloatWindow: FloatWindow
    private val delayTime = 5000L
    companion object{
        const val OPEN = 0
        const val CLOSE = 1
    }

    val mHandler =object :Handler(Looper.myLooper()!!){
        override fun handleMessage(msg: Message) {
            val color = if (msg.what == 1) Color.GREEN else Color.RED
            when(msg.what){
                1 -> {
                    tvStock1.text = msg.obj as String
                    tvStock1.setTextColor(color)
                }
                else -> {
                    tvStock2.text = msg.obj as String
                    tvStock2.setTextColor(color)
                }
            }
        }
    }

    //定时刷新任务
    private val mRefresh = object :Runnable{
        override fun run() {
            if (mFloatWindow != null && mFloatWindow.isShow){
                getStockIndex(1)
                getStockIndex(2)
            }
            mHandler.postDelayed(this,delayTime)
        }
    }


    // 获取股市证券指数
    private fun getStockIndex(type:Int){
        val message = Message.obtain()
        message.what = type
        message.obj = if (type == 1) {
            (num1++).toString()
        } else {
            num2+=2.2F
            num2.toString()
        }
        mHandler.sendMessage(message)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (intent != null){
            val type = intent.getIntExtra("type", OPEN)
            Log.d("service","$type")
            if (type == OPEN){
                if (!mFloatWindow.isShow){
                    tvStock1.text = "正在努力加载股指信息"
                    mFloatWindow.show(Gravity.LEFT or Gravity.TOP)
                }
            }else if ( type == CLOSE){
                if (mFloatWindow.isShow){
                    mFloatWindow.close()
                }
                stopSelf() // 停止自身服务
            }
        }

        return super.onStartCommand(intent, flags, startId)
    }

    override fun onCreate() {
        super.onCreate()
        mFloatWindow = FloatWindow(MainApplication.context)
        mFloatWindow.setLayout(R.layout.float_stock)
        tvStock1 = mFloatWindow.mContentView.findViewById(R.id.tv_sz_stock)
        tvStock2 = mFloatWindow.mContentView.findViewById(R.id.tv_sh_stock)
        mHandler.post(mRefresh)
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacks(mRefresh)
    }
}